using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using CodeStory.Core.UI;
using CodeStory.Core;
using CodeStory.Core.Providers;
using CodeStory.WebControls;
using CodeStory.Core.ObjectEntity;
using Activa.CoolStorage;

namespace CodeStory.WebApplication.Modules.Core.Admin
{
	[NavigationPortlet("CodeStory_Core_Admin_SitemapManager")]
	public partial class SitemapManager : Portlet,IHierarchyDataSelector
	{
		protected override void OnInit(EventArgs e) {
			Load += new EventHandler(SitemapManager_Load);
			btnRefresh.Click += new EventHandler(ButtonRefresh_Click);
			sitemapTreeGrid.RowCommand += new TreeGridCommandEventHandler(SitemapTreeGrid_RowCommand);
			tvMap.SelectedNodeChanged += new EventHandler(TreeViewMap_SelectedNodeChanged);
			btnOK.Click += new EventHandler(ButtonOK_Click);
			base.OnInit(e);
		}

		void ButtonOK_Click(object sender, EventArgs e) {
			//throw new Exception("The method or operation is not implemented.");
			SiteMapObject obj;
			string key = Request.QueryString["key"];
			bool flag = false;
			if (string.IsNullOrEmpty(key)) {
				obj = SiteMapObject.New();
			} else {
				obj = SiteMapObject.Read(key);
				flag = true;

			}
			if (flag) {
				string old = obj.NodeKey;
				CSList<SiteMapObject> list = SiteMapObject.List("ParentNodeKey=@ParentNodeKey", "@ParentNodeKey",old);
				foreach (SiteMapObject o in list) {
					o.ParentNodeKey = txtNodeKey.Text;
				}
				list.Save();
			}
			obj.NodeKey = txtNodeKey.Text;
			obj.Title = txtTitle.Text;
			//obj.Url = txtUrl.Text;
			obj.Url = cbbUrl.Text;
			obj.Description = txtDescription.Text;
			obj.ParentNodeKey = txtParentNodeKey.Text;
			obj.Save();
		}
		private void TreeViewMap_SelectedNodeChanged(object sender, EventArgs e) {
			TreeNode node = tvMap.SelectedNode;
			if (node == null) {
				return;
			}
			this.txtParentNodeKey.Text = node.Value;
		}
		private void BindTreeView() {
			TreeNode tn;
			tn = new TreeNode();
			tn.Text = "None";
			tn.Value = "";
			tvMap.Nodes.Add(tn);
			tn = new TreeNode();
			tn.Text = "Root";
			tn.Value = string.Empty;
			tvMap.Nodes.Add(tn);
			CreateTreeView(tn, SiteMap.RootNode.ChildNodes);
		}

		private void CreateTreeView(TreeNode tn, SiteMapNodeCollection siteMapNodeCollection) {
			foreach (SiteMapNode n in siteMapNodeCollection) {
				TreeNode treeNode = new TreeNode();
				treeNode.Text = n.Title;
				treeNode.Value = n.Key;
				tn.ChildNodes.Add(treeNode);
				if (n.HasChildNodes) {
					CreateTreeView(treeNode, n.ChildNodes);
				}
			}
		}
		void SitemapTreeGrid_RowCommand(object sender, TreeGridCommandEventArgs e) {
			//throw new Exception("The method or operation is not implemented.");
			switch (e.CommandName) {
				case "DeleteSitemap":
					DeleteSiteMapNode(e.CommandArgument.ToString());
					BindSitemap();
					break;
			}
		}

		private void DeleteSiteMapNode(string nodekey) {
			SiteMapObject obj = SiteMapObject.Read(nodekey);
			CSList<SiteMapObject> list = SiteMapObject.List("ParentNodeKey=@ParentNodeKey", "@ParentNodeKey", nodekey);
			foreach (SiteMapObject s in list) {
				DeleteSiteMapNode(s.NodeKey);
			}
			obj.Delete();
		}

		void ButtonRefresh_Click(object sender, EventArgs e) {
			//throw new Exception("The method or operation is not implemented.");
			IRefreshable r = SiteMap.Provider as IRefreshable;
			if (r != null) {
				r.Refresh();
			}
		}

		void SitemapManager_Load(object sender, EventArgs e) {
			//throw new Exception("The method or operation is not implemented.");
			if (!IsPostBack && !EditMode) {
				BindUrlComboBox();
				BindSitemap();
				BindTreeView();
				BindEditSiteMapObject();
			}
		}

		private void BindUrlComboBox() {
			CSList<PageObject> list = PageObject.List();
			cbbUrl.DataSource = list;
			cbbUrl.DataTextField = "Title";
			cbbUrl.DataValueField = "PageID";
			cbbUrl.ValueFormatString = "~/default.aspx?pageid={0}";
			cbbUrl.DataBind();
		}

		private void BindEditSiteMapObject() {
			string key = Request.QueryString["key"];
			if (string.IsNullOrEmpty(key)) return;
			SiteMapObject obj = SiteMapObject.Read(key);
			txtParentNodeKey.Text = obj.ParentNodeKey;
			txtNodeKey.Text = obj.NodeKey;
			txtTitle.Text = obj.Title;
			//txtUrl.Text = obj.Url;
			cbbUrl.Text = obj.Url;
			txtDescription.Text = obj.Description;
		}

		private void BindSitemap() {
			TreeGridDataSource source = new TreeGridDataSource(this);
			sitemapTreeGrid.DataSource = source;
			sitemapTreeGrid.DataBind();
		}

		#region IHierarchyDataSelector Members

		public ITreeGridDataObject[] SelectData(string path) {
			string parentStr = string.Empty;
			string key = string.Empty;
			if (!string.IsNullOrEmpty(path) && path.Trim() != "/") {
				int pos = path.LastIndexOf("/");
				key = path.Substring(pos + 1);
				parentStr = path.Substring(0, pos);
			}
			CSList<SiteMapObject> list = SiteMapObject.List("ParentNodeKey=@ParentNodeKey","@ParentNodeKey",key);
			ITreeGridDataObject[] result = new ITreeGridDataObject[list.Count];
			int i = 0;
			foreach (SiteMapObject obj in list) {
				ITreeGridDataObject data = new TreeGridDataObject(obj, obj.NodeKey, path, this);
				result[i++] = data;
			}
			return result;
		}

		public ITreeGridDataObject GetData(string path) {
			string parentStr = string.Empty;
			string key = string.Empty;
			if (!string.IsNullOrEmpty(path) && path.Trim() != "/") {
				int pos = path.LastIndexOf("/");
				key = path.Substring(pos + 1);
				parentStr = path.Substring(0, pos);
			}
			SiteMapObject obj = SiteMapObject.Read(key);
			return new TreeGridDataObject(obj, obj.NodeKey, parentStr, this);
		}

		ITreeGridDataObject[] IHierarchyDataSelector.SelectData(string path) {
			return this.SelectData(path);
		}

		ITreeGridDataObject IHierarchyDataSelector.GetData(string path) {
			return this.GetData(path);
		}

		#endregion
	}
}